<?php
/**
 * HomeController.php
 */

namespace RHX\Controller;

use Zend\Session\Container;

class HomeController extends RHXController {

    public function indexAction() {
	
	/* $update = new Container('update');
	  if (!$update->offsetExists('makeUpdate') || $update->offsetGet('makeUpdate') == false) {
	  // Mise à jour de la table rh_planning_semaine
	  $tablePlanning	 = new \RHX\Model\Table\PlanningSemaineTable($this->getAdapter());
	  $plannings	 = $tablePlanning->getAll();
	  $calculator	 = new \RHX\Model\Object\XCalculator($this->getAdapter());
	  $dayNames	 = array(
	  'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche',
	  );
	  foreach ($plannings as $planning) {
	  $heureTravaille	 = new \RHX\Helper\RHDateTime();
	  $heureRetard	 = new \RHX\Helper\RHDateTime();
	  $heureTravaille->setTime();
	  $heureRetard->setTime();
	  $nbRetard	 = 0;
	  $template	 = $planning->getTemplate();
	  //\Zend\Debug\Debug::dump($template, 'template en cours');
	  $employe	 = $planning->getEmploye();
	  foreach ($dayNames as $day) {
	  $data = $calculator->calculateOneDay($planning, $template, $day);
	  $heureTravaille->addHours($data->tempsDiff, false);
	  if ($data->retard) {
	  $heureRetard->addHours($data->tempsRetard, false);
	  $nbRetard++;
	  }
	  if ($data->anticipe) {
	  $heureRetard->addHours($data->tempsAnticipe, false);
	  $nbRetard++;
	  }
	  }
	  $planning->heure_prevues	 = $template->heure_prevues;
	  $planning->heure_travaille	 = \RHX\Helper\RHDateTime::toFloat($heureTravaille->totalHours());
	  $planning->heure_travail	 = $heureTravaille->totalHours();
	  $planning->heure_retard		 = $heureRetard->totalHours();
	  $planning->retard		 = $nbRetard;
	  $planning->coef			 = $employe->getCoefficient();
	  $planning->taux			 = $employe->getTaux();
	  $planning->hebdo		 = $employe->getHoraireHebdo();

	  // sauvegarde
	  $tablePlanning->save($planning);
	  }

	  // variable de session
	  $update->offsetSet('makeUpdate', true);

	  return array(
	  'message' => $plannings->count() . ' planning(s) mis a jour',
	  );
    } */
	// Mise à jour des plannings semaines
	$planningManager = new \RHX\Model\Manager\PlanningManager($this->getAdapter());
	$msg = $planningManager->updateAllPlanningSemaine();
	return array(
	    'msg' => $msg,
	);
    }

    public function changeSiteAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Données en post
	$post = $this->getRequest()
		->getPost();

	// Variables
	$siteId = $post->get('site');

	// Site
	$site = $this->getService('Table\SiteTable')
		->get($siteId);

	// Assignation
	$this->navigation->offsetSet('site', $site);

	return array(
	    'site' => $site,
	);
    }

    public function changePeriodeAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// données en post
	$post = $this->getRequest()
		->getPost();

	// Variables
	$periodeId = $post->get('periode');

	// Periode
	$periode = $this->getService('Table\PeriodeTable')
		->get($periodeId);

	// Assignation
	$this->navigation->offsetSet('periode', $periode);

	return array(
	    'periode' => $periode,
	);
    }

    public function updateAction(){
	$planningManager = new \RHX\Model\Manager\PlanningManager($this->getAdapter());
	$msg = $planningManager->updateAllPlanningSemaine();
	$this->layout('layout/ajax');
	return array(
	    'msg' => $msg,
	);
    }
}
